package com.xxl.shoppingapp.ui.home;

import android.support.annotation.NonNull;
import android.view.View;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xxl.shoppingapp.base.BaseRecyclerViewAdapter;
import com.xxl.shoppingapp.base.BaseRecyclerViewFragment;
import com.xxl.shoppingapp.entity.CommonProduct;
import com.xxl.shoppingapp.ui.ShopAppUIHelper;
import com.xxl.shoppingapp.ui.adapter.ProductAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuelian.xiong 2018/4/17
 * @Package com.xxl.shoppingapp.ui.home
 * @Title: FreshFragment
 * @Description: (食品列表页)
 * Copyright (c) 传化公路港物流有限公司版权所有
 * Create DateTime: 2018/4/17
 */
public class FoodFragment extends BaseRecyclerViewFragment {

    private ArrayList<CommonProduct> orders;
    static FoodFragment instance;

    public synchronized static FoodFragment getInstance() {
        if (instance == null) {
            instance = new FoodFragment();
        }
//        Bundle args = new Bundle();
//        args.putSerializable(TYPE, type);
//        instance.setArguments(args);
        return instance;
    }
    @Override
    protected void initView(View view) {
        super.initView(view);
        setTitle("食品");
        orders = new ArrayList<>();
    }

    @Override
    protected void initAdapter() {
        adapter = new ProductAdapter();
    }

    @Override
    protected void initData() {
        super.initData();
        data();
        adapter.setData(orders);
        adapter.setItemClickListener(new BaseRecyclerViewAdapter.ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ShopAppUIHelper.navDetail(instance);
            }
        });
        getSwipeRefreshLayout().setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getSwipeRefreshLayout().finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });

        getSwipeRefreshLayout().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                adapter.addData( data());
                getSwipeRefreshLayout().finishLoadMore(2000);
            }
        });
    }

    private List<CommonProduct> data() {
//
        for (int i = 0; i < 5; i++) {
            CommonProduct order = new CommonProduct();

            order.setProductName("豪尚豪牛排 10 片");

            orders.add(i, order);
        }

        return orders;
    }
}
